package com.renyi.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MybatisService;
import com.renyi.util.JSONUtils;

/**
 * 医生相关统计
 * @author zzf
 * 
 */
@Controller
public class DoctorStatisticsController extends BaseController {

	@Autowired
	private MybatisService mybatisService;

	// 获取处理和未处理病人数
	@RequestMapping("getHandlePatientStatistics")
	public void getHandlePatientStatistics(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
 
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data = this.mybatisService.procedureMybatisList("getHandlePatientStatistics",
					pMap);
			if(data!=null ){
				result.setCode(0); // 0：成功
				result.setData(data);
			} else{
				result.setCode(-1);
			}
			
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
 
	// 获取未处理病人列表
	@RequestMapping("getUnHandlePatientList")
	public void getUnHandlePatientList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String type = paramMap.get("type") != null ? paramMap.get("type").toString() : "";
			
			
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			
			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data =  null;			
			Long count = 0l;
			if("1".equals(type)){
				data = this.mybatisService.findMybatisList("getUnHandleHisPatientList", pMap);
				count = (Long)this.mybatisService.findIbatisListCount("getUnHandleHisPatientCount", pMap);
			}
			if("2".equals(type)){
				data = this.mybatisService.findMybatisList("getUnHandleAppPatientList", pMap);
				count = (Long)this.mybatisService.findIbatisListCount("getUnHandleAppPatientCount", pMap);
			}
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
	// 获取处理病人列表
	@RequestMapping("getHandlePatientList")
	public void getHandlePatientList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String startTime = paramMap.get("startTime") != null ? paramMap.get("startTime").toString() : "";
			String endTime =paramMap.get("endTime") != null ? paramMap.get("endTime").toString() : "";
			String type =paramMap.get("type") != null ? paramMap.get("type").toString() : "";
			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);
			
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			
			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);
			pMap.put("startTime", startTime);
			pMap.put("endTime", endTime);
			
			
			List<HashMap<String, Object>> data =  null;			
			Long count = 0l;
			if("1".equals(type)){
				data = this.mybatisService.findMybatisList("getHandleHisPatientList", pMap);
				count = (Long)this.mybatisService.findIbatisListCount("getHandleHisPatientCount", pMap);
			}
			if("2".equals(type)){
				data = this.mybatisService.findMybatisList("getHandleAppPatientList", pMap);
				count = (Long)this.mybatisService.findIbatisListCount("getHandleAppPatientCount", pMap);
			}
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
	

	// 获取诊金收入列表
	@RequestMapping("getDoctorFeeStatisticsList")
	public void getDoctorFeeStatisticsList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String startTime = paramMap.get("startTime") != null ? paramMap.get("startTime").toString() : "";
			String endTime =paramMap.get("endTime") != null ? paramMap.get("endTime").toString() : "";
			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);
			
			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			
			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);
			pMap.put("startTime", startTime);
			pMap.put("endTime", endTime);
			
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getDoctorFeeStatisticsList", pMap);
			Long count = this.mybatisService.findIbatisListCount("getDoctorFeeStatisticsCount", pMap);
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
	
	
	   //获取图文收入列表
		@RequestMapping("getDoctorRecordStatisticsList")
		public void getDoctorRecordStatisticsList(HttpServletRequest request, HttpServletResponse response) throws Exception {
			// 返回结果
			HttpResult result = new HttpResult();
			try {
				ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
				if (paramBean == null) {
					result.setCode(1003); // 0：成功
					this.ajaxResultJson(response, result);
					return;
				}
				Map<String, Object> paramMap = paramBean.getParam();// 用户ID
				String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
				String startTime = paramMap.get("startTime") != null ? paramMap.get("startTime").toString() : "";
				String endTime =paramMap.get("endTime") != null ? paramMap.get("endTime").toString() : "";
				Integer page = (int) (paramMap.get("page") != null
						? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
				Integer pageSize = (int) (paramMap.get("pageSize") != null
						? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);
				
				if ("".equals(uid)) {
					result.setCode(-1); // 0：成功
					result.setMsg("用户编号参数为空");
					this.ajaxResultJson(response, result);
					return;
				}
				
				HashMap<String, Object> pMap = new HashMap<String, Object>();
				Integer beginRow = (page - 1) * pageSize + 1;
				pMap.put("beginRow", beginRow);
				pMap.put("endRow", pageSize * page);
				pMap.put("uid", uid);
				pMap.put("startTime", startTime);
				pMap.put("endTime", endTime);
				
				List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getDoctorRecordStatisticsList", pMap);
				Long count = this.mybatisService.findIbatisListCount("getDoctorRecordStatisticsCount", pMap);
				count = count != null ? count : 0;
				result.setCode(0); // 0：成功
				result.setData(data);
				result.setRecordCount(count.intValue());
				this.ajaxResultJson(response, result);
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(-1); // 0：成功
				result.setMsg(e.getMessage());
				;// //错误消息(如无错误消息则留空)
				this.ajaxResultJson(response, result);
			}
		}
}
